#include "MarsPrecompile.h"
#include "MarsRenderObjectMgr.h"
#include "MarsRenderObject.h"

using namespace Graphics;

RenderObjectMgr* RenderObjectMgr::ms_pRenderObjectMgr = 0;

enum 
{
	Init_Array_Size = 100,
	Array_Inc_Size = 50,
};

void RenderObjectMgr::Destroy()
{
	SAFE_DELETE( ms_pRenderObjectMgr );
}

RenderObjectMgr::RenderObjectMgr()
	: m_iCurrentFreeIndex( 0 )
{

}

RenderObjectMgr::~RenderObjectMgr()
{
	Shutdown();
}

bool RenderObjectMgr::Initialize()
{
	m_arrRenderObjects.reserve( Init_Array_Size );

	for ( Core::uint32 i = 0; i < Init_Array_Size; ++i )
	{
		m_arrRenderObjects.push_back( new RenderObject() );
	}

	return true;
}

void RenderObjectMgr::Shutdown()
{
	for ( Core::uint32 i = 0; i < m_arrRenderObjects.size(); ++i )
	{
		SAFE_DELETE( m_arrRenderObjects[i] );
	}
}

RenderObject* RenderObjectMgr::GetFreeRenderObject() const
{
	if ( m_iCurrentFreeIndex >= m_arrRenderObjects.size() )
	{
		m_arrRenderObjects.reserve( m_arrRenderObjects.size() + Array_Inc_Size );

		// Add new free RenderObject into this array
		for ( Core::uint32 i = 0; i < Array_Inc_Size; ++i )
		{
			m_arrRenderObjects.push_back( new RenderObject() );
		}
	}

	return m_arrRenderObjects[ m_iCurrentFreeIndex++ ];
}
